/**
 * 
 */
package vcp.client.GUI.mvc.view.addons;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.border.BevelBorder;

import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 * 
 */
public class LeftNavigation extends UIAbstractView
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final HashMap <String, JButton> navigationTabMap;

	private String mainTab;
	private JButton currentTab;

	public LeftNavigation ( String[] navigationTabs )
	{
		super();

		this.navigationTabMap = new HashMap <>();
		this.loadNavtigationTabs(navigationTabs);

		this.currentTab = this.navigationTabMap.get(this.mainTab);
		this.resetView();
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	private void loadNavtigationTabs ( String[] navigationTabs )
	{
		for ( String tabName : navigationTabs )
		{
			if ( ! this.navigationTabMap.containsKey(tabName) )
			{
				JButton button = new JButton(tabName);

				button.setName(tabName);
				button.setFont(new Font("Tahoma", Font.PLAIN, 12));
				button.setFocusable(false);
				button.addActionListener(this);

				this.navigationTabMap.put(tabName, button);
				this.add(this.navigationTabMap.get(tabName));
			}
		}

		this.mainTab = navigationTabs[0];
	}

	private void toggleCurrentNavigationTab ( boolean enable )
	{
		this.currentTab.setEnabled(enable);
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	/**
	 * @see vcp.client.GUI.mvc.view.UIAbstractView#setupLayout()
	 */
	@Override
	protected void setupLayout ()
	{
		this.setLayout(new GridLayout(20, 0, 0, 0));
		this.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		this.setPreferredSize(new Dimension(120, 708));
	}

	/**
	 * @see vcp.client.GUI.mvc.view.UIAbstractView#setupListeners()
	 */
	@Override
	protected void setupListeners ()
	{
	}

	@Override
	public void resetView ()
	{
		this.toggleCurrentNavigationTab(true);
		this.currentTab = this.navigationTabMap.get(this.mainTab);
		this.toggleCurrentNavigationTab(false);
	}

	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton leftMenuButton = (JButton) actionEvent.getSource();

		String buttonName = leftMenuButton.getName();
		if ( ! buttonName.equalsIgnoreCase(Screen.LOG_OUT.toString()) )
		{
			this.controllerDelegate.onButtonPress(Screen.convert(buttonName));

			this.toggleCurrentNavigationTab(true);

			this.currentTab = leftMenuButton;

			this.toggleCurrentNavigationTab(false);
		}
		else
		{
			this.resetView();
			this.controllerDelegate.onButtonPress(null);
		}
	}

}
